<?php

/** 
 * @author kenny
 * 
 * 
 */
abstract class Nimblecms_Widget {
	
	/**
	 * @var Zend_Controller_Request_Abstract
	 */
	protected $_request;
	
	/**
	 * @var Zend_Controller_Response_Abstract
	 */
	protected $_response;
	
	/**
	 * @var Zend_View_Abstract
	 */
	protected $_view;
	
	/**
	 * Path of view helpers
	 * @var array
	 */
	protected $_helperPaths;
	
	/**
	 * Name of module that widget belong to
	 * @var string
	 */
	protected $_module;
	
	/**
	 * Name of widget
	 * @var string
	 */
	protected $_name;
	
	public function __construct($module, $name) {
		$this->_module = strtolower($module);
		$this->_name = strtolower($name);
		
		$front = Zend_Controller_Front::getInstance();
		
		$request = $front->getRequest();
		$response = $front->getResponse();
		
		$this->_request = clone $request;
		$this->_response = clone $response;
		
		$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$this->_view = clone $view->view;
		
		$this->_helperPaths = $this->_view->getHelperPaths();
		
		$this->_view->addHelperPath(APPLICATION_PATH . DS . 'modules' . DS . $this->_module . DS . 'views' . DS . 'helpers',
									$this->_module . '_View_Helper_'
									);
		$this->_view->addHelperPath(APPLICATION_PATH . DS . 'modules' . DS . $this->_module . DS . 'widgets' . DS . $this->_name,
									$this->_module . '_Widgets_' . $this->_name . '_'
									);
	}
	
	public function getName() {
		return $this->_name;
	}
	
	public function getModule() {
		return $this->_module;
	}
	
	public function __call($name, $arguments) {
		$this->_reset();
		
		if ($arguments != null && is_array($arguments) && count($arguments) > 0) {
			if ($arguments[0] != null && is_array($arguments[0])) {
				$this->_request->setParams($arguments[0]);
			}
		}
		
		$prepare = '_prepare' . ucfirst($name);
		if(method_exists($this, $prepare)) {
			$this->$prepare();
		}
		
		$name = strtolower($name);
		$path = APPLICATION_PATH . DS . 'modules' . DS . $this->_module . DS . 'widgets' . DS . ucfirst($this->_name);
		if(!file_exists($path . DS . $name . '.phtml')) {
			throw new Zend_Controller_Exception('Layout of widget ' . $this->_name . ' not found', 404);
			return;
		}
		$this->_view->addScriptPath($path);
		
		$file = $this->_view->getScriptPath(null) . $name . '.phtml';
		
		if($file != null || file_exists($file)) {
			$content = $this->_view->render($name . '.phtml');
			$this->_response->appendBody($content);
		}
		
		$body = $this->_response->getBody();
		$this->_reset();
		
		$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
		
		$view->setHelperPath(null);
		if($this->_helperPaths) {
			foreach($this->_helperPaths as $prefix=>$paths) {
				foreach($paths as $path) {
					$view->addHelperPath($path, $prefix);
				}
			}
		}
		
		return $body;
	}
	
	
	private function _reset() 
	{
		$params = $this->_request->getUserParams(); 
        foreach (array_keys($params) as $key) { 
            $this->_request->setParam($key, null); 
        } 
 
        $this->_response->clearBody();
        $this->_response->clearHeaders()->clearRawHeaders();
	}
	
	abstract protected function _prepareShow();
	
	protected function _prepareConfig() 
	{
		if (!Zend_Auth::getInstance()->hasIdentity()) {
			return;
		}
	}
}
